<?php include "header.php" ?>
<body onload="getTableCategory(0)">
<div id="wrapper">
    <?php include "main.php" ?>

    <div id="page-wrapper">

            <div class="container-fluid">
                <div class="row">
                    <div class="col-lg-12">
                        <h1 class="page-header">
                            Danh mục sản phẩm
                        </h1>
                        <ol class="breadcrumb">
                            <li>
                                <i class="fa fa-dashboard"></i>  <a href="<?php echo base_url() ?>admin/dashboard">Dashboard</a>
                            </li>
                            <li class="active">
                                <i class="fa fa-table"></i> <a href="<?php echo base_url() ?>admin/category">Danh Mục Sản Phẩm</a>
                            </li>
                            <li class="pull-right">
                                <button class="btn btn-primary btn-xs" onclick="getCategoryNews(0); lightbox_open('lightCreateCateNews', 'fadeCreateCateNews')"><span class="glyphicon glyphicon-plus"></span> Thêm Mới</button>
                            </li>
                        </ol>
                    </div>
                </div>
                <table class="table table-striped table-bordered table-hover">
                    <thead>
                    <tr>
                        <th>#</th>
                        <th>Nhóm tin</th>
                        <th>Mô tả</th>
                        <th>Trạng thái</th>
                        <th></th>
                    </tr>
                    </thead>
                    <tbody id="divTableCategory">
                    </tbody>
                </table>
            </div>
    </div>
</div>

<!-- input Create Control Panel -->
<div class="light row" id="lightCreateCateNews" style="width: 60%; height: inherit; left: 30%; top: 10%;">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">Thêm Mới Danh Mục</h3>
        </div>
        <div class="panel-body">
            <div class="row" style="margin-bottom: 10px">
                <div id="divStepCateNews" class="form-group col-lg-12">
                    <label class="label label-default">Tên danh mục (*)</label>
                    <input style="margin-top: 2px;" type="text" class="form-control" id="txtCreateCateNews">
                    <label class="label label-default">Mô tả (*)</label>
                    <textarea class="form-control" id="txtShortDesCreateCateNews"></textarea>
                    <label class="label label-default">Thông tin chi tiết (*)</label>
                    <textarea class="form-control" id="txtDesCreateCateNews"></textarea>
                    <label class="checkbox-inline">
                        <input type="checkbox" id="inlineCheckboxCateNews" checked> Hiển thị
                    </label>
                </div>
                <div id="divStepCateNews" class="col-lg-12">
                    <span class="label label-success" style="font-size: 14px"><b>Danh mục bạn đã chọn: </b></span>
                    <div id="divSelectedCatNews" style="font-size: 20px;">Top</div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12" id="divSubCateNewsStep" style="border: 1px solid grey">
                    <input type="hidden" id="hiddenCategoryNews" value="0">
                    <div class="row">
                        <div class="col-lg-2"><img class="img-responsive" src="<?php echo base_url()."assets/images/folder.png"; ?>"></div><div class="col-lg-10"><a style='cursor: pointer' onclick='getSubCategoryNews(0)'>Top</a></div>
                    </div>
                    <div class="row">
                        <div class="col-lg-12" id="divCategoryNews"></div>
                    </div>
                </div>
            </div>
            <div class="row" style="margin: 5px; text-align: center">
                <span class="btn btn-success" style="margin: 5px" onclick="createCateNews();">Thêm danh mục</span>
                <span class="btn btn-success" style="margin: 5px" onclick="lightbox_close('lightCreateCateNews', 'fadeCreateCateNews');">Đóng cửa sổ</span>
            </div>
        </div>
    </div>
</div>
<div class="row fade" id="fadeCreateCateNews" onClick="lightbox_close('lightCreateCateNews', 'fadeCreateCateNews');"></div>
<!-- end input Create Control Panel -->


<!-- input Edit Control Panel -->
<div class="light row" id="lightEditCateNews" style="width: 60%; height: inherit; left: 30%; top: 10%;">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">Chỉnh Sửa Danh Mục Tin Tức</h3>
        </div>
        <div class="panel-body">
            <div class="row" style="margin-bottom: 10px">
                <div id="divStepCateNews" class="form-group col-lg-12">
                    <label class="label label-default">Tên danh mục (*)</label>
                    <input style="margin-top: 2px;" type="text" class="form-control" id="txtEditCateNews">
                    <label class="label label-default">Mô tả</label>
                    <textarea class="form-control" rows="3" id="txtShortDesEditCateNews"></textarea>
                    <label class="label label-default">Thông tin chi tiết (*)</label>
                    <textarea class="form-control" rows="3" id="txtDesEditCateNews"></textarea>
                    <label class="checkbox-inline">
                        <input type="checkbox" id="inlineCheckboxEditCateNews" checked> Hiển thị
                    </label>
                </div>
                <div id="divStepEditCateNews" class="col-lg-12">
                    <span class="label label-success" style="font-size: 14px"><b>Danh mục bạn đã chọn: </b></span>
                    <div id="divSelectedEditCatNews" style="font-size: 20px;"></div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12" id="divSubEditCateNewsStep" style="border: 1px solid grey">
                    <input type="hidden" id="hiddenEditCategoryNews">
                    <div class="row">
                        <div class="col-lg-2"><img class="img-responsive" src="<?php echo base_url()."assets/images/folder.png"; ?>"></div><div class="col-lg-10"><a style='cursor: pointer' onclick='getSubEditCategoryNews(0)'>Top</a></div>
                    </div>
                    <div class="row">
                        <div class="col-lg-12" id="divEditCategoryNews"></div>
                    </div>
                </div>
            </div>
            <div class="row" style="margin: 5px; text-align: center">
                <input type="hidden" id="hiddenEditCategoryNewsID">
                <span class="btn btn-success" style="margin: 5px" onclick="updateCateNewsDB()">Cập Nhật</span>
                <span class="btn btn-success" style="margin: 5px" onclick="lightbox_close('lightEditCateNews', 'fadeEditCateNews');">Đóng cửa sổ</span>
            </div>
        </div>
    </div>
</div>
<div class="row fade" id="fadeEditCateNews" onClick="lightbox_close('lightEditCateNews', 'fadeEditCateNews');"></div>
<!-- end input Edit Control Panel -->

<script language="JavaScript" type="text/javascript">
    function getTableCategory(id){
        var url = "<?php echo base_url() ?>";
        $.ajax({
            url: url+"admin_category/getParentCategory/"+id,
            success: function(x){
//                alert(x);
                $('#divTableCategory').html(x);
            }
        });
    }

    function getCategoryNews(id){
        var url = "<?php echo base_url() ?>";
        $.ajax({
            url: url+"admin_category/getAddParentCategory/"+id,
            success: function(x){
//                alert(x);
                $('#divCategoryNews').html(x);
            }
        });
    }

    function getEditCategoryNews(id){
        var url = "<?php echo base_url() ?>";
        $.ajax({
            url: url+"admin_category/getEditParentCategory/"+id,
            success: function(x){
//                alert(x);
                $('#divEditCategoryNews').html(x);
            }
        });
    }

    function getSubCategoryNews(id){
        var id = id;
        //alert(id);
        $('#hiddenCategoryNews').val(id);
        if(id != '0'){
            var url = "<?php echo base_url() ?>";
            var dataString = "id="+id;
            $.ajax({
                type: "POST",
                url: url+"admin_category/getSubCategoryNews",
                data: dataString,
                success: function(x){
                    //alert(x);
                    $('#divSelectedCatNews').html(x);
                }
            });
        }
        else{
            $('#divSelectedCatNews').html("Top");
        }
    }

    function getSubEditCategoryNews(id){
        var id = id;
        //alert(id);
        $('#hiddenEditCategoryNews').val(id);
        if(id != '0'){
            var url = "<?php echo base_url() ?>";
            var dataString = "id="+id;
            $.ajax({
                type: "POST",
                url: url+"admin_category/getEditSubCategoryNews",
                data: dataString,
                success: function(x){
                    //alert(x);
                    $('#divSelectedEditCatNews').html(x);
                }
            });
        }
        else{
            $('#divSelectedEditCatNews').html("Top");
        }
    }

    function createCateNews(){
        var name = $('#txtCreateCateNews').val();
        var shortDes = $('#txtShortDesCreateCateNews').val();
        var des = CKEDITOR.instances.txtDesCreateCateNews.getData();
        if(name == "" || shortDes == "" || des == ""){
            alert("Các thông đánh dấu * là các thông tin bắc buộc!")
        }
        else{
            var id = $('#hiddenCategoryNews').val();
            var active = 1;
            if(!$('#inlineCheckboxCateNews').is(':checked') ){ //kiem tra neu checkbox khong duoc check
                active = 0;
            }

            //alert(id)
            var url = "<?php echo base_url() ?>";
            var dataString = "name="+name+"&id="+id+"&active="+active+"&des="+des+"&shortDes="+shortDes;
            //alert(dataString)
            $.ajax({
                type: "POST",
                url: url+"admin_category/createCategoryNews",
                data: dataString,
                success: function(x){
                    //alert(x);
                    if(x == '0'){
                        alert("Tạo danh mục thành công!");
                        lightbox_close('lightCreateCateNews', 'fadeCreateCateNews');
                        getTableCategory(0)
                    }
                    else{
                        alert(x);
                    }
                }
            });
        }
    }

    function updateCategoryNews(id){
        //alert(id);
        var id = id;
        $('#hiddenEditCategoryNewsID').val(id);
        var url = "<?php echo base_url() ?>";
        var dataString = "id="+id;
        $.ajax({
            type: "POST",
            url: url+"admin_category/getInfoCateNewsID",
            data: dataString,
            success: function(x){
                //alert(x);
                var data = x.split("?=");

                $('#txtEditCateNews').val(data[0]);
                $('#txtShortDesEditCateNews').val(data[1]);
                CKEDITOR.instances.txtDesEditCateNews.setData(data[2]);

                $('#hiddenEditCategoryNews').val(data[3]);
                //alert($('#hiddenEditCategoryNews').val());
                if(data[3] == 0){
                    $('#divSelectedEditCatNews').html("Top");
                }
                else{
                    getSubEditCategoryNews(data[3]);
                }

                if(data[4] == 0){
                    $('#inlineCheckboxEditCateNews').prop("checked", false);
                }
                else{
                    $('#inlineCheckboxEditCateNews').prop("checked", true);
                }
                getEditCategoryNews(0);
                lightbox_open('lightEditCateNews', 'fadeEditCateNews');
            }
        });
    }

    function updateCateNewsDB(){
        var name = $('#txtEditCateNews').val();
        var shortDes = $('#txtShortDesEditCateNews').val();
        var des = CKEDITOR.instances.txtDesEditCateNews.getData();
        if(name == "" || shortDes == "" || des == ""){
            alert("Các thông đánh dấu * là các thông tin bắc buộc!")
        }
        else{
            var id = $('#hiddenEditCategoryNewsID').val();
            var parentid = $('#hiddenEditCategoryNews').val();
            var active = 1;
            if(!$('#inlineCheckboxEditCateNews').is(':checked')){
                active = 0;
            }
            if(id == parentid){
                alert("Xin vui lòng chọn danh mục khác với danh mục hiện tại...");
            }
            else{
                var url = "<?php echo base_url() ?>";
                var dataString = "id="+id+"&name="+name+"&parentid="+parentid+"&active="+active+"&des="+des+"&shortDes="+shortDes;
                //alert(dataString);
                $.ajax({
                    type: "POST",
                    url: url+"admin_category/updateCategoryNews",
                    data: dataString,
                    success: function(x){
                        //alert(x);
                        if(x == '0'){
                            alert("Cập nhật danh mục thành công!");
                            getTableCategory(0);
                            lightbox_close('lightEditCateNews', 'fadeEditCateNews');
                        }
                        else{
                            alert(x);
                        }
                    }
                });
            }
        }
    }

    function deleteCategoryNews(id){
        //alert(id);

        var id = id;
        var url = "<?php echo base_url() ?>";
        var dataString = "id="+id;
        $.ajax({
            type: "POST",
            url: url+"admin_category/getCateNewsParentID",
            data: dataString,
            success: function(x){
                //alert(x);
                if(x != '0'){
                    var check = confirm("Danh mục này hiện có "+x+" danh mục con.\n\nThao tác này sẽ xóa bao gồm danh mục cần xóa và tất cả các danh mục bên trong của nó.\n\n" +
                        "Bạn chắc chắn muốn xóa?");
                }
                else{
                    var check = confirm("Bạn chắc chắn muốn xóa?");
                }

                if(check == true){
                    $.ajax({
                        url: url+"admin_category/deleteCategory/"+id,
                        success: function(x){
//                                alert(x);
                            if(x != 1){
                                alert("Xóa thành công! \nCác bài viết trong thư mục này đã được chuyển sang thư mục "+x);
                                getTableCategory(0);
                            }
                            else{
                                alert("Đã xảy ra lỗi! Xin vui lòng thử lại...");
                            }
                        }
                    });
                }
                else{
                    return;
                }
            }
        });
    }
</script>

<script>
    CKEDITOR.replace( 'txtDesCreateCateNews' );
    CKEDITOR.replace( 'txtDesEditCateNews' );
</script>

<?php include "footer.php" ?>

</body>

</html>